package com.example.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringValueResolver;

import javax.sql.DataSource;

/**
 * bean 注入属性的另外一种做法
 */
@Configuration
public class SpringConfig5 implements EmbeddedValueResolverAware {

    private StringValueResolver resolver;

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.resolver = resolver;
    }

    @Bean
    public DataSource dataSourceTest()  {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(resolver.resolveStringValue("${db.url}"));
        dataSource.setUsername(resolver.resolveStringValue("${db.username}"));
        dataSource.setPassword(resolver.resolveStringValue("${db.password}"));
        dataSource.setDriverClassName(resolver.resolveStringValue("${db.driverClass}"));
        return dataSource;
    }


    @Bean
    public DataSource dataSourceDev()  {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(resolver.resolveStringValue("${db.url}"));
        dataSource.setUsername(resolver.resolveStringValue("${db.username}"));
        dataSource.setPassword(resolver.resolveStringValue("${db.password}"));
        dataSource.setDriverClassName(resolver.resolveStringValue("${db.driverClass}"));
        return dataSource;
    }

    @Bean
    public DataSource dataSourceProd() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(resolver.resolveStringValue("${db.url}"));
        dataSource.setUsername(resolver.resolveStringValue("${db.username}"));
        dataSource.setPassword(resolver.resolveStringValue("${db.password}"));
        dataSource.setDriverClassName(resolver.resolveStringValue("${db.driverClass}"));
        return dataSource;
    }


}



